//
//  ChartDateBaseView.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 09.04.25.
//

import SwiftUI

struct ChartDateBaseView: View {
    
    @State private var message = "Initial Message"
    
    @State var trendTitle: String = ""
    @State var averageTitle: String = ""
    @State var categoryTitle: String = ""
    @Binding var date: Date
    @State var dateType: DateSelectType
    @Binding var categoryType: CategoryType
    @Binding var totalAmount: Double
    @Binding var averageAmount: Double
    @Binding var trendData: [(day:String, amount:Double)]
    @Binding var pieData: [ChartPieItem]
    @Binding var koiPieData:[(value:Double, color:Color)]
    @Binding var categoryAmountList: [CategoryAmount]
    @Binding var category: AccountingCategory?
    
    @State var selectedItem: AccountingCategory? = nil
    @State var openSubView: Bool = false
    
    var loadData: (Date) -> Void
    
    var body: some View {
        ScrollView {
            // 日期类型选择器
            DateSelectView(
                date: $date,
                dateType: dateType,
                isFullWidth: true,
                leftClick: { date in
                    loadData(date)
                },
                rightClick: { date in
                    loadData(date)
                }
            ).padding(.horizontal, 20)
            // 趋势视图
            ChartTrendView(
                trendTitle: $trendTitle,
                averageTitle: $averageTitle,
                totalAmount: $totalAmount,
                averageAmount: $averageAmount,
                data:$trendData
            )
            .padding(EdgeInsets(top: 10, leading: 20, bottom: 5, trailing: 20))
            // 类别视图
            ChartCategoryView(
                date: $date,
                categoryTitle: $categoryTitle,
                pieData: $pieData,
                koiPieData: $koiPieData,
                categoryAmountList: $categoryAmountList,
                dateSelectType: dateType,
                onCategoryItemClick: { category in
                    selectedItem = category
                    openSubView = true
                    print("onCategoryItemClick date \(date)")
                }
            )
            .navigationDestination(isPresented: $openSubView, destination: {
                if let category = selectedItem {
                    // 如果是在二级类别统计页面，打开单个类别详情页
                    if category.hasSubCategory == false {
                        ChartCategoryDetailView(date: date, category: category, dateSelectType: dateType)
                    } else {
                        ChartSubView(date: date, category: category, dateSelectType: dateType)
                    }
                } else {
                    EmptyView()
                }
            })
            .padding(EdgeInsets(top: 5, leading: 20, bottom: 5, trailing: 20))
            // 底部占位
            Color.clear.frame(height: 90)
        }
        .onChange(of: categoryType) { _ in
            trendTitle = getTrendTitle()
            averageTitle = getAverageTitle()
            categoryTitle = getCategoryTitle()
            loadData(date)
        }
        .onDisappear {
            KoiEvent.homeReload.removeObserver(self)
        }
        .onAppear {
            trendTitle = getTrendTitle()
            averageTitle = getAverageTitle()
            categoryTitle = getCategoryTitle()
            KoiEvent.homeReload.addObserver { _ in
                loadData(date)
            }
        }
    }
    
    private func getTrendTitle() -> String {
        var title = "本周"
        switch dateType {
        case .week:
            title = "本周"
        case .month:
            title = "本月"
        case .year:
            title = "本年"
        }
        return "\(title)\(getTypeText())"
    }
    
    private func getAverageTitle() -> String {
        switch dateType {
        case .week:
            return "日均\(getTypeText())"
        case .month:
            return "日均\(getTypeText())"
        case .year:
            return "月均\(getTypeText())"
        }
    }
    
    private func getCategoryTitle() -> String {
        return "类别\(getTypeText())统计"
    }
    
    private func getTypeText() -> String {
        if categoryType == .income {
            return "收入"
        } else {
            return "支出"
        }
    }
}

//#Preview {
//    ChartDateBaseView()
//}
